package com.ruoyi.medical.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.medical.domain.MedicineOrder;
import com.ruoyi.medical.service.IMedicineOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单管理Controller
 * 
 * @author WGH
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/medical/medicineOrder")
public class MedicineOrderController extends BaseController
{
    @Autowired
    private IMedicineOrderService medicineOrderService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:medicineOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(MedicineOrder medicineOrder)
    {
        startPage();
        List<MedicineOrder> list = medicineOrderService.selectMedicineOrderList(medicineOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:medicineOrder:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MedicineOrder medicineOrder)
    {
        List<MedicineOrder> list = medicineOrderService.selectMedicineOrderList(medicineOrder);
        ExcelUtil<MedicineOrder> util = new ExcelUtil<MedicineOrder>(MedicineOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('medical:medicineOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(medicineOrderService.selectMedicineOrderById(id));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('medical:medicineOrder:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MedicineOrder medicineOrder)
    {
        int result = medicineOrderService.insertMedicineOrder(medicineOrder);
        if (result > 0) {
            // 插入成功，返回生成的订单ID
            return AjaxResult.success("新增订单成功", medicineOrder.getId());
        } else {
            return AjaxResult.error("新增订单失败");
        }
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('medical:medicineOrder:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MedicineOrder medicineOrder)
    {
        return toAjax(medicineOrderService.updateMedicineOrder(medicineOrder));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('medical:medicineOrder:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(medicineOrderService.deleteMedicineOrderByIds(ids));
    }
}
